package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.EmployeeDTO;
import com.tm.dto.EmployeesDTO;
import com.tm.entity.EmployeeEntity;
import com.tm.enums.StatusCodeEnum;
import com.tm.service.EmployeeService;
import com.tm.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author huangp
 * @create 2023-04-05 9:36
 */
@RestController
@RequestMapping("employee")
@CrossOrigin
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    
    @PostMapping("/login")
    public Result loginApi(HttpServletRequest request, @RequestBody EmployeesDTO employeesDTO){
        String username = employeesDTO.getUsername();
        if (StringUtils.isBlank(employeesDTO.getUsername()) || StringUtils.isBlank(employeesDTO.getPassword())){
                return Result.error(StatusCodeEnum.PSERROR);
        }
        QueryWrapper<EmployeeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",employeesDTO.getUsername());
        EmployeeEntity employee = employeeService.getOne(wrapper);
        if(employee.getUsername() == null){
            log.error("访问登录接口时输入端账号不存在,员工账号[{}]",username);
            return Result.error(StatusCodeEnum.PSERRORY);
        }
        //判断密码是否正确
        //加密前端传过来的密码
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(employeesDTO.getPassword().getBytes());
        //密码不匹配
        if(md5DigestAsHex.equals(employeesDTO.getPassword())){
            log.error("登录接口校验密码失败,员工账号[{}]",username);
            return Result.error(StatusCodeEnum.PSERRORYE);
        }
        Integer status = employee.getStatus();
        if(!status.equals(1)){
            log.error("登录接口校验密码失败,员工账号[{}]",username);
            return Result.error(StatusCodeEnum.PSERROR_STATUS);
        }
        request.getSession().setAttribute("emoloyee",employee.getId());
        employee.setPassword(null);
        return Result.success(employee);
    }

    @GetMapping("page")
    public Result getEmployeePage(@RequestParam(required = false,defaultValue = "1") Integer page,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                 EmployeeDTO employeeDTO){
        Page<EmployeeEntity> page2 = new Page<>(page,pageSize);
        QueryWrapper<EmployeeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(employeeDTO.getName()),"name",employeeDTO.getName());
        Page<EmployeeEntity> page1 = employeeService.page(page2,queryWrapper);
        return Result.success(page1);
    }

    @PostMapping
    public Result addEmployee(@RequestBody EmployeeEntity employeeEntity){
        Boolean flag = employeeService.addEmployee(employeeEntity);
        return Result.success(flag);
    }
    //登出接口
    @PostMapping("logout")
    public Result logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result queryEmployeeById(@PathVariable("id") Long id){
        EmployeeEntity employeeEntity = employeeService.getById(id);
        return Result.success(employeeEntity);
    }

    @DeleteMapping
    public Result batchDelete(@RequestParam("ids") List<Long> ids){
        Boolean flag = employeeService.removeByIds(ids);
        return Result.success(flag);
    }

    @PutMapping
    public Result editDish(@RequestBody EmployeeEntity employeeEntity ){
        UpdateWrapper<EmployeeEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(employeeEntity.getId() != null,"id",employeeEntity.getId());
        return Result.success(employeeService.update(employeeEntity,updateWrapper));
    }

    public static void main(String[] args) {

        String slat = "tm17";
        String str = "998855";
        String s = DigestUtils.md5DigestAsHex(str.getBytes());
        System.out.println(s);

    }

}
